<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
</head>

<body>
	<script>
		function timeTable(x, y) {
			var array = [];
			x = x ? Number(timeToNumFmt(x)) : false;
			y = y ? Number(timeToNumFmt(y)) : false;
			for (var i = 0, a = '', b = ''; i < 25; i++) {
				a = Number(i + '.00');
				b = Number(i + '.50');
				if (x || y) {
					if (a >= x && a <= y) {
						array.push(i + ':00');
					}
					if (b >= x && b <= y) {
						if (i === 24) {
							array.push(i + ':00');
						} else {
							array.push(i + ':30');
						}
					}
				} else {
					if (i === 24) {
						array.push(i + ':' + '00');
					} else {
						array.push(i + ':' + '00');
						array.push(i + ':' + '30');
					}
				}
			}
			return array;
		}

		function timeToNumFmt(src, type) {
			if (!src) return src;
			if (!type) type = 1;

			var reg = [/\:/g, /\.30?/g, /\./g, /\:50?/g];
			var rule = ['.', '.50', ':', ':30'];

			if (typeof src === 'string') {
				src = [src];
			}

			if (typeof src === 'object' || typeof src === 'array') {
				var array = [];
				for (var i = 0; i < src.length; i++) {
					if (type === 1) {
						array.push(src[i].replace(reg[0], rule[0]).replace(reg[1], rule[1]));
					} else if (type === 2) {
						array.push(src[i].replace(reg[2], rule[2]).replace(reg[3], rule[3]));
					}
				}
				return array.length === 1 ? array[0] : array;
			}

			return src;
		}

		var util = {
			timeTable: timeTable,
			timeToNumFmt: timeToNumFmt,
		};

		function showTime(x, y) {
			if (!x) return false;
			var temp = [];
			var array = [];

			if (!y) y = [];

			x = util.timeTable(x[0], x[1]);
			x = util.timeToNumFmt(x);

			for (var i = 0, a = null; i < y.length; i++) {
				a = y[i] ? y[i].split(/\-/) : [];
				a = util.timeTable(a[0], a[1]);
				temp = a.concat(temp);
			}

			temp = temp.sort();
			temp = util.timeToNumFmt(temp);
			for (var j = 0; j < x.length; j++) {
				if (temp && temp.indexOf(x[j]) <= -1) {
					array.push(x[j]);
				}
			}
			return array.length > 0 ? array : false;
		}

	</script>

	<script>
		console.log('==============分割线==============');

		(function () {

			console.log('bookTimetable [15:30-19:30]:', bookTimetable('15:30-19:30'));
			function bookTimetable(data) {
				var rc = { src: [], time: [] };
				data = data ? data.split(/\-/) : [];
				if (!data[0] || !data[1]) return rc;

				rc.src = util.timeTable(data[0], data[1]);
				if (!rc.src) return rc;

				for (var i = 0, len = rc.src.length; i < len; i++) {
					rc.time.push(Number(util.timeToNumFmt(rc.src[i])));
				}
				return rc;
			}

			var _allTimetablePrice = [["7:30-9:30", "9:30-17:30", "17:30-21:30"], [5000, 5000, 7500]];
			console.log('_allTimetablePrice:', _allTimetablePrice);
			console.log('allTimetablePrice:', allTimetablePrice(_allTimetablePrice));
			function allTimetablePrice(data) {
				var rc = { src: [], time: {}, node: {} };
				if (!data || !data[0] || !data[1]) return rc;

				var timeArray = data[0];
				var priceArray = data[1];
				for (var i = 0, v = null, len = timeArray.length; i < len; i++) {
					v = timeArray[i];
					if (!v) continue;

					v = v.split(/\-/);
					if (!v[0] || !v[1]) continue;

					v = util.timeTable(v[0], v[1]);
					rc.src[i] = [];
					for (var j = 0, d = null, len2 = v.length; j < len2; j++) {
						d = v[j];
						if (!d) continue;

						rc.src[i].push(d);
						rc.time[Number(util.timeToNumFmt(d))] = priceArray[i];
						if (j === 0) {
							rc.node[Number(util.timeToNumFmt(d))] = priceArray[i];
						}
						if (j + 1 === v.length) {
							rc.node[Number(util.timeToNumFmt(d))] = priceArray[i];
						}
					}
				}
				return rc;
			}

			console.log(
				'countTimePrice:',
				countTimePrice(
					bookTimetable('15:30-19:30').time,
					allTimetablePrice(_allTimetablePrice).time
				) / 100
			);
			function countTimePrice(data, src) {
				var rc = 0;
				if (!data) return rc;

				for (var i = 0, v = null, len = data.length; i < len; i++) {
					v = data[i];
					if (!v) continue;
					if (i + 1 < len) {
						if (src[v])
							rc += Number(src[v]);
					}
				}
				return rc / 2;
			}
		})()

	</script>

</body>

</html>